package com.seuic.hid_demo.util.ext

import androidx.annotation.IntegerRes
import java.io.UnsupportedEncodingException
import java.nio.charset.Charset


/**
 * @Desc
 * @author KP
 * @date 2020/12/17 13:52
 * @version
 */

/**
 * 校验一个字符是否是汉字
 *
 * @param c
 * 被校验的字符
 * @return true代表是汉字
 */
fun Char.isChineseCharUTF8(): Boolean {
    return try {
        toString().toByteArray(Charsets.UTF_8).size > 1
    } catch (e: UnsupportedEncodingException) {
        e.printStackTrace()
        false
    }
}

fun String.toHidList(charset: Charset): List<String> {
    return toByteArray(charset).toHex(charset).toInt(16).toString().toList()
}

/*
     * 中文转unicode编码
     */
fun String.toUnicodeHidList(): List<String> {
    val utfBytes = toCharArray()
    var unicodeBytes = ""
    for (i in utfBytes.indices) {
        var hexB = utfBytes[i].toByte().toHex()
        if (hexB.length <= 2) {
            hexB = "00$hexB"
        }
        unicodeBytes = unicodeBytes.plus(hexB)
    }
    return unicodeBytes.toList()
}

fun main(args: Array<String>) {
    println("你".toHidList(Charset.forName("GB2312")))
    println("你".toHidList(Charset.forName("GBK")))

}